package com.wang.lotterysystem.common.pojo;

import com.wang.lotterysystem.common.errorcode.ErrorCode;
import com.wang.lotterysystem.common.errorcode.GlobalErrorCodeConstants;
import lombok.Data;
import org.springframework.util.Assert;

@Data
public class CommonResult<T> {
    private Integer code;
    private T data;
    private String msg;
    public static <T> CommonResult<T> success(T data){
        CommonResult<T> commonResult = new CommonResult<>();
        commonResult.setCode(GlobalErrorCodeConstants.SUCCESS.getCode());
        commonResult.setMsg(GlobalErrorCodeConstants.SUCCESS.getMsg());
        commonResult.setData(data);
        return commonResult;
    }
    public static <T> CommonResult<T> error(ErrorCode errorCode){
        Assert.isTrue(!errorCode.getCode().equals(GlobalErrorCodeConstants.SUCCESS.getCode()),"code必须是错误码");//校验code是否是成功状态
        CommonResult<T> commonResult = new CommonResult<>();
        commonResult.setCode(errorCode.getCode());
        commonResult.setMsg(errorCode.getMsg());
        return commonResult;
    }
    public static <T> CommonResult<T> error(String msg,Integer code){
        Assert.isTrue(!code.equals(GlobalErrorCodeConstants.SUCCESS.getCode()),"code必须是错误码");
        CommonResult<T> commonResult = new CommonResult<>();
        commonResult.setCode(code);
        commonResult.setMsg(msg);
        return commonResult;
    }
}